package com.lxmes.sales.service.impl;

import java.util.List;
import com.lxmes.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lxmes.common.utils.StringUtils;
import com.lxmes.common.utils.bean.BeanValidators;
import com.lxmes.common.exception.ServiceException;
import javax.validation.Validator;
import com.lxmes.sales.mapper.AssetKucunMapper;
import com.lxmes.sales.domain.AssetKucun;
import com.lxmes.sales.service.IAssetKucunService;

/**
 * 资产库存
Service业务层处理
 *
 * @author zhangzhiyu
 * @date 2024-08-28
 */
@Service
public class AssetKucunServiceImpl implements IAssetKucunService
{
    @Autowired
    private AssetKucunMapper assetKucunMapper;

    @Autowired
    protected Validator validator;
    /**
     * 查询资产库存

     *
     * @param id 资产库存主键
     * @return 资产库存

     */
    @Override
    public AssetKucun selectAssetKucunById(Long id)
    {
        return assetKucunMapper.selectAssetKucunById(id);
    }

    /**
     * 查询资产库存列表
     *
     * @param assetKucun 资产库存

     * @return 资产库存

     */
    @Override
    public List<AssetKucun> selectAssetKucunList(AssetKucun assetKucun)
    {
        return assetKucunMapper.selectAssetKucunList(assetKucun);
    }

    /**
     * 新增资产库存

     *
     * @param assetKucun 资产库存

     * @return 结果
     */
    @Override
    public int insertAssetKucun(AssetKucun assetKucun)
    {
        assetKucun.setCreateTime(DateUtils.getNowDate());
        return assetKucunMapper.insertAssetKucun(assetKucun);
    }

    /**
     * 修改资产库存

     *
     * @param assetKucun 资产库存

     * @return 结果
     */
    @Override
    public int updateAssetKucun(AssetKucun assetKucun)
    {
        return assetKucunMapper.updateAssetKucun(assetKucun);
    }

    /**
     * 批量删除资产库存

     *
     * @param ids 需要删除的资产库存主键
     * @return 结果
     */
    @Override
    public int deleteAssetKucunByIds(Long[] ids)
    {
        return assetKucunMapper.deleteAssetKucunByIds(ids);
    }

    /**
     * 删除资产库存
信息
     *
     * @param id 资产库存
主键
     * @return 结果
     */
    @Override
    public int deleteAssetKucunById(Long id)
    {
        return assetKucunMapper.deleteAssetKucunById(id);
    }
    @Override
    public String imporIteminfo(List<AssetKucun> assetKucunList, boolean updateSupport, String operName) {
        if (StringUtils.isNull(assetKucunList) || assetKucunList.size() == 0)
        {
            throw new ServiceException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (AssetKucun assetKucun : assetKucunList)
        {
            try
            {
                BeanValidators.validateWithException(validator, assetKucun);
                assetKucun.setCreateBy(operName);
                assetKucun.setUpdateBy(operName);
                this.insertAssetKucun(assetKucun);
                successNum++;
                successMsg.append("<br/>" + successNum + "数据导入成功");
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/> 数据导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
